package com.hnqcgc.controller;

import com.hnqcgc.dao.IAdminDao;
import com.hnqcgc.service.IAdminService;
import com.hnqcgc.service.impl.AdminServicelmpl;
import com.hnqcgc.vo.TAdmin;
import org.junit.Test;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

@WebServlet("/AdminServlet")
public class AdminServlet  extends BaseServlet {

    IAdminService service = new AdminServicelmpl();


    @Override
    public String home(HttpServletRequest req, HttpServletResponse resp) {

        List<TAdmin> list=new ArrayList<TAdmin>();

        String adminName= req.getParameter("adminName");

        if ("".equals(adminName)||adminName==null){
            list=service.queryAll();
        }else {
            list=service.queryByNameLike(adminName);
        }

        req.getSession().setAttribute("admins", list);

        return "f:UserControl";
    }

    //登录方法
    public void login(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //设置编码格式
        req.setCharacterEncoding("utf-8");
        resp.setCharacterEncoding("utf-8");


        String name = req.getParameter("adminName");
        String password = req.getParameter("adminPassword");
        System.out.println(name);
        System.out.println(password);


        IAdminService service = new AdminServicelmpl();
        TAdmin admin = service.login(name, password);
        if (admin != null) {

            req.getSession().setAttribute("admin", admin);
            resp.sendRedirect("index.jsp");
        } else {
            System.out.println("登录失败");
        }
    }





    public String add(HttpServletRequest req, HttpServletResponse resp) {
        String adminName = req.getParameter("adminName");
        String adminPassword = req.getParameter("adminPassword");
        String phone = req.getParameter("phone");
        String email = req.getParameter("email");
        TAdmin admin = new TAdmin();
        admin.setAdminName(adminName);
        admin.setAdminPassword(adminPassword);
        admin.setAdminRole(0);
        admin.setPhone(phone);
        admin.setEmail(email);

        //
        int result = service.adminInsert(admin);
        if (result > 0) {
            System.out.println("新增成功");
        } else {
            System.out.println("新增失败");
        }
        //
            return "r:AdminServlet";

        }

    public String deleteById(HttpServletRequest req, HttpServletResponse resp){

        String adminId=req.getParameter("adminId");
        System.out.println("您要删除的编号为："+adminId);

        int result = service.adminDelete(Long.parseLong(adminId));
        if (result > 0) {
            System.out.println("新增成功");
        } else {
            System.out.println("新增失败");
        }
        //
        return "r:AdminServlet";

    }

    public String updateById(HttpServletRequest req, HttpServletResponse resp){
        String adminId=req.getParameter("adminId");
        String adminName=req.getParameter("adminName");
        String adminPassword=req.getParameter("adminPassword");
        String adminRole=req.getParameter("adminRole");
        String phone=req.getParameter("phone");
        String email=req.getParameter("email");

        TAdmin admin=new TAdmin();
        admin.setAdminId(Long.parseLong(adminId));
        admin.setAdminName(adminName);
        admin.setAdminPassword(adminPassword);
        admin.setAdminRole(Long.parseLong(adminRole));
        admin.setPhone(phone);
        admin.setEmail(email);


        int result = service.adminUpdate(admin);
        if (result > 0) {
            System.out.println("修改成功");
        } else {
            System.out.println("修改失败");
        }
        return "r:AdminServlet";
    }

}


